import Vue from 'vue'
import Router from 'vue-router'

const login = () => import('../components/login.vue');
const home = () => import('../components/home.vue');
const register = () => import('../components/register.vue');
const forget = () => import('../components/forget.vue');
const records = () => import('../components/records.vue');
const exportData = () => import('../components/exportData.vue');


Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      redirect:'/login'
    },
    {
      path:'/login',
      name: 'login',
      component: login,
    },
    {
      path:'/forget',
      name: 'forget',
      component: forget,
    },
    {
      path:'/register',
      name: 'register',
      component: register,
    },
    {
      path:'/records',
      name: 'records',
      component: records,
    },
    {
      path:'/home',
      name:'home',
      component:home,
    },
    {
      path:'/exportData',
      name:'exportData',
      component:exportData,
    }
  ]
})

// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = Router.prototype.push
Router.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}

router.beforeEach((to,from,next) => {
  console.log(to,from);
  let email = localStorage.getItem('email');
  if(to.path == '/register' || to.path == '/forget' || to.path == '/records' || to.path == '/exportData'){
    next();
    return;
  }
  if(to.path != '/login' && !email){
    router.push({
      name:'login'
    })
  }
  else{
    next();
  }
  
})


export default router;